package com.example.chatroom.controller;

import com.example.chatroom.common.AjaxResult;
import com.example.chatroom.common.UserSessionTools;
import com.example.chatroom.entity.FriendEntity;
import com.example.chatroom.entity.MessageSession;
import com.example.chatroom.entity.MessageSessionUserItem;
import com.example.chatroom.entity.UserEntity;
import com.example.chatroom.service.MessageService;
import com.example.chatroom.service.MessageSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class MessageSessionController {
    @Autowired
    private MessageSessionService messageSessionService;

    @Autowired
    private MessageService messageService;

    // todo: 获取当前用户的会话列表
    @RequestMapping("/sessionList")
    public AjaxResult getMessageSessionList(HttpServletRequest request){
        List<MessageSession> messageSessionList = new ArrayList<>();
        // 1，从当前会话中获取到当前用户的userId
        UserEntity user = UserSessionTools.getLoginUser(request);
        if(user == null || user.getUser_id() <= 0)
            return  AjaxResult.fail(-1,"当前用户未登录",messageSessionList);
        // 2,根据 userId 获取到当前用户存在的会话列表，返回一组SessionId
        List<Integer> sessionIdList = messageSessionService.getSessionIdsByUserId(user.getUser_id());
        for (int sessionId: sessionIdList) {
            // 3,遍历 sessionIdList 查询每个会话中存在的用户信息
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            // 4,根据 SessionId 获取到会话中存在的用户信息（除去当前用户的用户信息）
            List<FriendEntity> friends = messageSessionService.getFriendsBySessionId(sessionId, user.getUser_id());
            messageSession.setFriends(friends);
            // 5,遍历 sessionIdList 查询每个会话中的最后一条消息 TODO
            String lastMessage = messageService.getLastMessageBySessionId(sessionId);
            if(lastMessage == null){
                messageSession.setLastMessage("");
            }else{
                messageSession.setLastMessage(lastMessage);
            }
            // 6,将每个会话messageSession添加至会话列表messageSessionList
            messageSessionList.add(messageSession);
        }
        // 7,最终返回一个会话列表信息messageSessionList
        return AjaxResult.success(messageSessionList);
    }

    // todo: 创建会话
    @RequestMapping("/addMessageSession")
    public AjaxResult addMessageSession(int toUserId,HttpServletRequest request){
        // 1，从当前会话中获取到当前用户的userId
        UserEntity user = UserSessionTools.getLoginUser(request);
        if(user == null || user.getUser_id() <= 0)
            return  AjaxResult.fail(-1,"当前用户未登录");
        MessageSession messageSession = new MessageSession();
        // 2，根据新建的 messageSession 来获取 sessionId,并往会话表MessageSession新增会话记录
        messageSessionService.addMessageSession(messageSession);
        // 3,会话用户关联表MessageSessionUser新增对应记录
        MessageSessionUserItem item1 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserId(toUserId);
        messageSessionService.addMessageSessionUser(item1);
        MessageSessionUserItem item2 = new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserId(user.getUser_id());
        messageSessionService.addMessageSessionUser(item2);
        HashMap<String,Integer> hashMap = new HashMap<>();
        hashMap.put("sessionId",messageSession.getSessionId());
        return AjaxResult.success(hashMap);
    }

}
